#include "apue.h"

int globvar = 6; // external variable in initialized date
char buf[] = "a write to stdout\n";

int main() {
    int var = 88; // automatic variable on the stack
    pid_t pid;

    if (write(STDOUT_FILENO, buf, sizeof(buf) - 1) != sizeof(buf) - 1) {
        err_sys("before fork\n");
    }

    if ((pid = fork()) < 0) {
        err_sys("fork error\n");
    } else if (pid == 0) {
        ++globvar;
        ++var;
    } else {
        sleep(2);
    }

    printf("pid = %ld, glob = %d, var = %d\n",
                (long)getpid(), globvar, var);
    return 0;
}